package Utils

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"unsafe"
)

func StructToBytes(pStruct interface{}) []byte  {
	buf := &bytes.Buffer{}

	if err := binary.Write(buf, binary.LittleEndian, pStruct); err != nil {
		fmt.Println(err)
		return nil
	}

	return buf.Bytes()
}

type SliceMock struct {
	addr uintptr
	len int
	cap int
}

func StructToBytesUp(pStruct interface{}) []byte  {
	//bs := &pStruct
	Len := unsafe.Sizeof(pStruct)
	testBytes := &SliceMock{
		addr: uintptr(unsafe.Pointer(&pStruct)),
		cap: int(Len),
		len: int(Len),
	}
	buf := *(*[]byte)(unsafe.Pointer(testBytes))
	return buf
}